package com.sonicvision.resourcetracker.employee;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sonicvision.resourcetracker.exceptions.ServiceException;

@Controller
public class EmployeeExperienceController {

	@Autowired
	private EmployeeExperienceService expService;

	@RequestMapping(value = "/addexperience", method = RequestMethod.GET)
	public ModelAndView addEmpExp(EmployeeExperience employeeExp) {

		ModelAndView mav = new ModelAndView();

		System.out.println("I am inside get");
		mav.addObject("employeeExperience", employeeExp);
		mav.setViewName("AddExperience");

		return mav;

	}

	@RequestMapping(value = "/addexperience", method = RequestMethod.POST)
	public ModelAndView addEmpExp(@Valid EmployeeExperience employeeExp,
			BindingResult br) {

		System.out.println("I am inside post");

		ModelAndView mav = new ModelAndView();

		try {
			if (br.hasErrors()) {
				System.out.println("i am inside if");
				mav.addObject("employeeExperience", employeeExp);
				mav.setViewName("AddExperience");
				return mav;
			} else {
				System.out.println("i am inside else");
				expService.addEmpExp(employeeExp);
				mav.addObject("employeeexperience", employeeExp);
				mav.setViewName("AcknowledgeExperience");
				return mav;

			}
		} catch (ServiceException e) {
			System.out.println("i am inside catch");
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/updateexperience", method = RequestMethod.POST)
	public ModelAndView updateEmpExp(
			@Valid EmployeeExperience employee_Experience_Id, BindingResult br) {

		System.out.println("I am inside post");

		ModelAndView mav = new ModelAndView();

		try {
			if (br.hasErrors()) {
				System.out.println("i am inside if");
				mav.addObject("employeeExperience", employee_Experience_Id);
				mav.setViewName("UpdateExperience");
				return mav;
			} else {
				System.out.println("i am inside else");
				expService.updateEmpExp(employee_Experience_Id);
				mav.addObject("employeeexperience", employee_Experience_Id);
				mav.setViewName("AcknowledgeExperience");
				return mav;

			}
		} catch (ServiceException e) {
			System.out.println("i am inside catch");
			e.printStackTrace();
			return null;
		}
	}

	@InitBinder
	public void initBinder(WebDataBinder webDataBinder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}
}
